<?php

    class MetricsTracker {
        public $token;
        public $host = 'http://api.mixpanel.com/';
        public function __construct($token_string) {
            $this->token = $token_string;
        }
        function track($event, $properties=array()) {
            $params = array(
                'event' => $event,
                'properties' => $properties
                );
    
            if (!isset($params['properties']['token'])){
                $params['properties']['token'] = $this->token;
            }
            $url = $this->host . 'track/?data=' . base64_encode(json_encode($params));
            //you still need to run as a background process
            exec("curl '" . $url . "'");
            
            //$ch = curl_init();
//            
//            curl_setopt($ch, CURLOPT_URL,$url);
//            curl_setopt($ch, CURLOPT_FAILONERROR,1);
//            curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
//            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
//            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//            $sendSMS = curl_exec($ch);
//            curl_close($ch);
        }
    }
    
    if(isset($_GET['mobile'])){
        $number = $_GET['mobile'];
    }
    
    if(isset($_GET['text'])){
        $data = $_GET['text'];
    }
    
    //var_dump($_GET);
    //echo $data;
    $split_data = array();
    $split_data['pub_shortcode'] = substr($data, 6, 2);
    $split_data['deal_id'] = substr($data, 8);
    $publishershort = $split_data['pub_shortcode'];
    //var_dump($split_data);
    
    $publishers = array("BP" => '4db9436799b4a',"FL" => '4e7c05337de77');
    
    foreach($publishers as $pubKey => $pubValue){
        if($pubKey == $split_data['pub_shortcode']){
            $apikey = $pubValue;
            break;
        }
    }
    
    //var_dump($apikey);
    
    $api_pod = new Pod('api');
    $api_pod->page = 1;
    $api_pod->search = 1;
    $api_pod->findRecords('id DESC', -1, "t.api_key ='$apikey'");
    $api_exists = $api_pod->getTotalRows();
    if($api_exists > 0){
    	while($api_pod->fetchRecord()){
    	  $client_name = $api_pod->get_field('name');
    	  $client_name = explode('_',$client_name);
    	  $client_name = $client_name[0];    
    	}
    }
    
    //var_dump($client_name);
    
    $pod_item = new Pod('deal');
    $deal_info = array();
    $where .= 't.id='.$split_data['deal_id']." && "; //this is for fetching coupon codes of deal with pod id
    $where .= 't.camp_end >= '.fix_date();
    $pod_item->findRecords('id DESC', -1,$where);
    $coupon_exists = $pod_item->getTotalRows();
    if($coupon_exists == 1){
        while($pod_item->fetchRecord()){
            $deal_info['coupon'] = $pod_item->get_field('couponcode');
			$deal_info['title'] = $pod_item->get_field('name');
			$deal_info['expireson'] = $pod_item->get_field('camp_end');
			$deal_info['address'] = $pod_item->get_field('deal_address');
			$deal_info['brand'] = $pod_item->get_field('brand.name');
        }
    }
    
    /* Run Google Event */
	$brand = urlencode(ucwords($deal_info['brand']));
	$event = urlencode('SMS CouponRequest'); //could add publisher name to this as well
	$deal = urlencode($deal_info['title']);
    
    $metrics = new MetricsTracker("192cdc0889effbe78d4c0a90da4aa508");
    $metrics->track($event, array('brand'=>$brand, 'deal'=>$deal, 'mobile'=>$number, 'publisher'=>$apikey));
    /*
    $url = "http://buzzr.in/wp-content/plugins/buzzr_api/mixpanel_api.php";
    
    $send_string = "brand={$brand}&event={$event}&deal={$deal}";
    
    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    if(!$fp){
        fclose($fp);
        $deal_info['expireson']=date("m/d/y",strtotime($deal_info['expireson']));
    	$deal_info['address']=strip_tags($deal_info['address']);
        //var_dump($deal_info);
    	//$response_text='Coupon code: ".$deal_info['coupon']."Deal: ".$deal_info['title']."Expires: ".$deal_info['expireson']."';
    	$response_text="Coupon code: ".$deal_info['coupon']."\nDeal: ".$deal_info['title']."\nExpires: ".$deal_info['expireson']."\nAddress: ".$deal_info['address']."\nVisit Buzzr.in for more coupons and deals!";
    	//$response_text="Coupon code: ".$deal_info['coupon']."Deal: ".$deal_info['title']."Expires: ".$deal_info['expireson'];
    	echo $response_text;
    }else{
        $out = "POST ".$parts['path']." HTTP/1.1\r\n";
        $out.= "Host: ".$parts['host']."\r\n";
        $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out.= "Content-Length: ".strlen($send_string)."\r\n";
        $out.= "Connection: Close\r\n\r\n";
        if (isset($post_string)) $out.= $send_string;
        
        fwrite($fp, $out);
        fclose($fp);*/
        
        $deal_info['expireson']=date("m/d/y",strtotime($deal_info['expireson']));
    	$deal_info['address']=strip_tags($deal_info['address']);
        //var_dump($deal_info);
    	//$response_text='Coupon code: ".$deal_info['coupon']."Deal: ".$deal_info['title']."Expires: ".$deal_info['expireson']."';
    	$response_text="Coupon code: ".$deal_info['coupon']."\nDeal: ".$deal_info['title']."\nExpires: ".$deal_info['expireson']."\nAddress: ".$deal_info['address']."\nVisit Buzzr.in for more coupons and deals!";
    	//$response_text="Coupon code: ".$deal_info['coupon']."Deal: ".$deal_info['title']."Expires: ".$deal_info['expireson'];
    	echo $response_text;
    //}
    
?>